<?php
/**
 * @link http://www.anabas.tk/awc/wiki
 *
 * @package Aba Sulution
 * @subpackage Aba_Core
 * @since Aba Core 1.0.1 beta
 */

defined( 'ABSPATH' ) or die(/*Nothing...*/);

function awc_test(){
    return false;
}

function awc_defined(){
    return defined( 'AWC_CORE' );
}

function awc_import( $folder ){
    $files = glob( $folder . '/*', GLOB_BRACE);
    foreach ($files as $filename)
    {
        if ( !is_dir( $filename ) ){
            if ( substr( $filename, -4 ) == '.php' )
                require_once ( $filename );
        }
        else {
            awc_import( $filename );
        }
    }
}

function awc_import_js($folder, $type = 'head'){
    $files = glob( $folder . '/*');
    foreach ($files as $filename)
    {
        /*if ( !is_dir( $filename ) )
            require_once ( $filename );
        else
            awc_import( $filename );*/
    }
}

function activated_plugin_hook(){
    
    $active_plugins = get_option('active_plugins');
    
    $this_plugin = AWC_BASENAME_FILE;
    
    $this_plugin_key = array_search($this_plugin, $active_plugins);
    
    if($this_plugin_key){
        
        array_splice( $active_plugins, $this_plugin_key, 1 );
        
        array_unshift( $active_plugins, $this_plugin );
        
        update_option( 'active_plugins', $active_plugins );
    }
}

add_action( 'activated_plugin', 'activated_plugin_hook' , 10001 );

function awc_load(){    
    awc_import( AWC_CLASSES_DIR );        
    do_action( 'awc_loaded' );
}

//private

function _awc_get_tmpl( $backtrace, $name, $base_dir = 'tmpl' ){
    $file = array_shift( $backtrace );
	
	$file = $file['file'];
    
    $backtrace = explode( '/', plugin_basename( $file ) );
    
    $plugin_dir_name = array_shift( $backtrace );
    
    $file = WP_PLUGIN_DIR . '/' . $plugin_dir_name . '/' . $base_dir . '/' . ( !empty( $name ) ? $name : 'default' ) . '.php';
    
    $file = apply_filters( 'awc_plugin_template_file', $file );
    
    if ( file_exists( $file ) ) return $file;
    
    return false;
}

//#public

/**
* awc_extmpl
* 
* Return src file template.
* 
* @access   public
* @name     string name template
* @return   string src file template. 
*/

function awc_extmpl( $name = 'default' ){
    
    $backtrace = debug_backtrace();
    
    return _awc_get_tmpl( $backtrace, $name );
}

function awc_imtmpl( $name = 'default', $variables = array() ){
    
    $backtrace = debug_backtrace();
    
    $file = _awc_get_tmpl( $backtrace, $name );
    
    if ( !$file ) return false;
    
    if ( is_array( $variables ) ){
        extract( $variables, EXTR_PREFIX_ALL, '' );
        $configs = new stdClass();
        foreach( $variables as $key => $value )
            $configs->{$key} = $value;
    }
    
    return include( $file );
}

function awc_is_abstract( $classname ){
    $ref = new ReflectionClass( $classname );
    return $ref->isAbstract();
}

function awc_can_pluginload( $classname ){
    $ref = new ReflectionClass( $classname );
    if ( !$ref->isSubclassOf( 'IAWC_API' ) )
        return false;
    if ( $classname == 'AWC_Core' )
        return false;
    
    if ( $ref->isAbstract() ) 
        return false;
    return true;
}

function awc_reg_mod( $class_name ){
    global $awcore;
    if ( defined( 'AWC_AUTOLOADED' ) ){
        throw new Exception( "You can't use this function when 'AWC_AUTOLOAD' is TRUE" );
    }
    if ( !isset( $awcore->plugins ) || !is_array( $awcore->plugins ) )
        $awcore->plugins = array();
    if ( !class_exists( $class_name ) ){
        throw new Exception( $class_name . ' is not exists.' );
    }    
    if ( isset( $awcore->plugins[$class_name] ) ){
        throw new Exception( "Extention '$class_name' is registed." );
    }    
    $mod = new stdClass();
    $mod->classname     = $class_name;
    
    $ref = new ReflectionClass( $class_name );
    
    if ( $ref->isSubclassOf( 'AWC_Widget' ) ){
        $mod->type = 'widget';
    }
    else if( $ref->isSubclassOf( 'AWC_Dashboad' ) ){
        $mod->type = 'dashboard';
    }
    else if( $ref->isSubclassOf( 'AWC_Page_Options' ) ){
        $mod->type = 'page_options';
    }
    else{
        $mod->type = 'other';
    }
    //$mod->type = strtolower( str_replace( 'AWC_', '', $class_name ) );
    if ( isset( $mod->type ) )
        $awcore->plugins[$class_name] = $mod;    
}

//functions entity

function _awc_call( $action, $callback, $priority = 10, $accepted_args = 1, $access = true ){
    if ( is_bool( $priority ) ){ 
        $access = $priority; 
        $priority = 10;
    }
    if ( is_bool( $accepted_args ) ){
        $access = $accepted_args;
        $accepted_args = 1;
    }
    if ( !$access ) return false;
    add_action( $action, $callback, $priority, $accepted_args );    
}

function awc_reg_ajax( $action, $callback, $priority = 10, $accepted_args = 1, $access = true ){
    _awc_call( "wp_ajax_$action", $callback, $priority, $accepted_args, $access );
}

function awc_reg_ajax_nopriv( $action, $callback, $priority = 10, $accepted_args = 1, $access = true ){
    _awc_call( "wp_ajax_nopriv_$action", $callback, $priority, $accepted_args, $access );
}

function awc_reg_init( $callback, $priority = 10, $accepted_args = 1, $access = true ){
    _awc_call( "init", $callback, $priority, $accepted_args, $access );
}

function awc_reg_admin_init( $callback, $priority = 10, $accepted_args = 1, $access = true ){
    _awc_call( "admin_init", $callback, $priority, $accepted_args, $access );
}

function awc_reg_script( $handle, $src = null, $access = true ){
    if ( !$access ) return false;
    if ( $handle == 'media' ){        
        wp_enqueue_media( is_array( $src )? $src : array() );
    }
    else if ( !empty( $src ) ){
        wp_enqueue_script( $handle, $src );
    }
    else{
        wp_enqueue_script( $handle );
    }
}

function awc_reg_style( $handle, $src = null, $access = true ){
    if ( !$access ) return false;
    add_action( 'wp_enqueue_scripts', function(){
        if ( !empty( $src ) ){
            wp_enqueue_style( $handle, $src );
        }
        else{
            wp_enqueue_style( $handle );
        }
    } );
}

if ( !function_exists( 'debug' ) ):
function debug( $object, $die = true ){
    echo "<pre>";
    print_r( $object );
    echo "</pre>";
    if ( $die ) die;
}
endif;

function awc_enid($id){
    try{
        $rep = apply_filters( 'awc_icr_rep', array( "123456789", "qwrtyuiop" ) );
        if ( !is_array( $rep[0] ) )
            $a = str_split( $rep[0], 1 );
        if ( !is_array( $rep[1] ) )
            $b = str_split( $rep[1], 1 );
        $hex = strtolower( dechex($id + AWC_ICR_ID) );    
        return str_replace( $a, $b, $hex );
    }
    catch(Exception $e){
        return $id;
    }    
    
}

function awc_deid($hex_id){
    try{
        $rep = apply_filters( 'awc_icr_rep', array( "123456789", "qwrtyuiop" ) );
        if ( !is_array( $rep[0] ) )
            $a = str_split( $rep[0], 1 );
        if ( !is_array( $rep[1] ) )
            $b = str_split( $rep[1], 1 );
        $hex_id = strtoupper( str_replace( $b, $a, $hex_id ) );
        return hexdec($hex_id) - AWC_ICR_ID;
    }
    catch(Exception $e){
        return $hex_id;
    }
}